import { AuthGuard } from '@nestjs/passport'
import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  UseFilters,
  UsePipes,
  ValidationPipe,
  UseGuards,
  Query,
  Req,
} from '@nestjs/common'
import { PropositionService } from './proposition.service'
import { CreatePropositionDto, CommentType, GetCommentType, LikeType } from './dto/create-proposition.dto'
// import { UpdatePropositionDto } from './dto/update-proposition.dto'
import { HttpExceptionFilter } from 'src/filter/httpRequestCatch'
import { ReqUser } from '../strategy/dto/strategy.dto'
import { Request } from 'express'
@Controller('proposition')
@UseFilters(HttpExceptionFilter) //异常过滤器
@UsePipes(new ValidationPipe()) //nest验证管道
export class PropositionController {
  constructor(private readonly propositionService: PropositionService) {}

  //创建帖子
  @Post()
  @UseGuards(AuthGuard('jwt'))
  async create(@Body() createPropositionDto: CreatePropositionDto) {
    return await this.propositionService.create(createPropositionDto)
  }

  /* 查找当前贴吧下的所有帖子 */
  @Get('getProposition/:barId')
  async findAll(@Param('barId') barId: string, @Query() info: { page: number; pagesize: number; name: string }) {
    const result = await this.propositionService.findAll(barId, info)
    return {
      data: result,
      code: 200,
      message: '查询成功',
    }
  }

  /* 删除帖子 */
  @Delete('/delete/:id')
  @UseGuards(AuthGuard('jwt'))
  async deleteProposition(@Param('id') id: string) {
    const result = await this.propositionService.deleteProposition(id)

    return {
      data: { title: result.title },
      code: 200,
      message: '删除成功',
    }
  }

  /* 发布评论 */
  @Post('comment')
  @UseGuards(AuthGuard('jwt'))
  async Pubcomment(@Body() comment: CommentType, @Req() req: Request) {
    const id = (req.user as ReqUser)._id
    const result = await this.propositionService.Pubcomment(comment, id)
    return { data: result, code: 200, message: '发布成功' }
  }

  /* 获取当前帖子下的所有评论 */
  @Get('getComment')
  async getCommentByPropositionID(@Query() query: GetCommentType) {
    const result = await this.propositionService.getComment(query, null)
    return { data: result, code: 200, message: '查询成功' }
  }
  /* 获取当前帖子下的所有评论 (登录状态) */
  @Get('getCommentByToken')
  @UseGuards(AuthGuard('jwt'))
  async getCommentByToken(@Req() req: Request, @Query() query: GetCommentType) {
    const user = req.user as ReqUser
    const result = await this.propositionService.getComment(query, user._id)
    return { data: result, code: 200, message: '查询成功' }
  }

  /* 删除评论 */
  @Delete('deleteComment/:commentID')
  @UseGuards(AuthGuard('jwt'))
  async deleteComment(@Req() req: Request, @Param('commentID') commentID: string) {
    const userID = (req.user as ReqUser)._id
    return await this.propositionService.deleteComment({ user: userID, comment: commentID })
  }

  /* 收藏帖子 */
  @Get('collect/:propositionID')
  @UseGuards(AuthGuard('jwt'))
  async collectProposition(@Req() req: Request, @Param('propositionID') propositionID: string) {
    const userid = (req.user as ReqUser)._id
    return await this.propositionService.collectProposition({ user: userid, proposition: propositionID })
  }

  /* 点赞(给帖子或者评论) */
  @Get('like')
  @UseGuards(AuthGuard('jwt'))
  async clickLike(@Query() info: LikeType, @Req() req: Request) {
    const userId = (req.user as ReqUser)._id
    return this.propositionService.like({ user: userId, comment: info.comment, proposition: info.proposition })
  }

  /* 获取帖子详情 (已登录) */
  @Get('propositionInfoByToken')
  @UseGuards(AuthGuard('jwt'))
  async propositionInfoByToken(@Req() req: Request, @Query() info: { propositionID: string }) {
    const user = req.user as ReqUser
    return await this.propositionService.getPropositionInfoByToken(info.propositionID, user._id)
  }

  /* 获取帖子详情 (未登录) */
  @Get('propositionInfoNotToken')
  async propositionInfoNotToken(@Query() info: { propositionID: string }) {
    return await this.propositionService.getPropositionInfoByToken(info.propositionID, null)
  }
}
